import { createRouter, createWebHistory } from 'vue-router'
import Index from '../views/Index.vue'
import store from '@/store'
const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/index',
    name: 'Index',
    component: Index,
    children: [
      {
        path: '/document',
        name: 'document',
        component: () => import('../views/Document.vue')
      }
    ]
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/user/home.vue'),
    children: [
      {
        path: 'home',
        name: 'Userhome',
        component: () => import('../views/user/courses.vue')
      },
      {
        // 所有课程
        path: 'course',
        name: 'course',
        component: () => import('../views/course/allCourses.vue')
      },
      {
        //我的课程
        path: 'mycourse',
        name: 'mycourse',
        component: () => import('../views/course/courseDetail.vue'),
        children: [
          {
            //我的课程_公告
            path: 'announcement',
            name: 'announcement',
            component: () => import('../views/course/announcement.vue')
          },
          {
            //我的课程_资料
            path: 'courseMaterial',
            name: 'courseMaterial',
            component: () => import('../views/course/courseMaterial.vue')
          },
          {
            //我的课程_讨论
            path: 'courseDiscuss',
            name: 'courseDiscuss',
            component: () => import('../views/course/courseDiscuss.vue')
          },
          {
            //我的课程_任务
            path: 'courseTask',
            name: 'courseTask',
            component: () => import('../views/course/courseTask.vue')
          },
          {
            //我的课程_批改
            path: 'work',
            name: 'work',
            component: () => import('../views/course/work.vue')
          },
          {
            //我的课程_章节
            path: 'chapters',
            name: 'chapters',
            component: () => import('../views/course/chapters.vue')
          },
          {
            //我的课程_作业
            path: 'coursehomework',
            name: 'coursehomework',
            component: () => import('../views/course/courseHomeWork.vue')
          },
          {
            //我的课程_做答
            path: 'answer',
            name: 'answer',
            component: () => import('../views/course/answer.vue')
          },
          {
            //班级成员管理
            path: 'memberShip',
            name: 'memberShip',
            component: () => import("../views/course/memberShip.vue")
          },
          {
            //课程信息管理
            path: 'configeration',
            name: 'configeration',
            component: () => import('../views/course/courseConfigeration.vue')
          }
        ]
      },
      {
        //个人中心
        path: 'center',
        name: 'center',
        component: () => import('../views/user/personalCenter.vue'),
        children: [
          {
            path: '/space',
            name: 'space',
            component: () => import('../views/user/spaceCourse.vue')
          },
          {
            path: 'information',
            name: 'information',
            component: () => import('../views/user/information.vue')
          }
        ]
      },
      // 章节学习中心
      {
        path: 'studentStudy',
        name: 'studentStudy',
        component: () => import('../views/course/studentStudy.vue')
      }
    ]
  },
  //管理员部分路由
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/admin/home.vue'),
    children: [
      {
        path: 'userManage',
        name: 'userManage',
        component: () => import('../views/admin/userManage.vue')
      },
    ]
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/admin/login.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    //浏览器的默认行为 返回时返回原先位置
    if (savedPosition) {
      return { savedPosition }
    }
    else {
      return { top: 0, behavior: 'smooth' }
    }
    //也可以返回一个相对某个元素的偏移
    // return {
    //   el:document.getElementById('course-container'),
    //   top:-10
    // }
  }
})

//路由导航守卫
router.beforeEach((to, from, next) => {
  //to将要访问的路径
  //from代表从哪一个路径跳转而来
  //next放行
  if (to.path == '/home' || to.path == '/adminLogin') { return next(); }
  else {
    //获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) { return next('/home') }
    if (to.path == '/admin/userManage') {
      if (store.getters.getRole == 'admin')
        next()
      else {
        router.go(-1)
        return next(false)
      }
    }
    else {
      next()
    }
  }
})

export default router
